package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.common.lang.Level;
import net.zjitc.common.ResponseResult;
import net.zjitc.common.page.CategoryPageData;
import net.zjitc.entity.Category;
import net.zjitc.exception.CategoryException;
import net.zjitc.mapper.CategoryMapper;
import net.zjitc.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 毛钰深
 * @Create 2022/1/4 13:15
 */
@Service
@Transactional
public class ICategoryService implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    private List<Category> categoryList;

    @Override
    public ResponseResult getCategories(Integer type) throws CategoryException {
        if (type==null){
            type=3;
        }
        Level level= Level.getLevel(type-1);
        if (level==null){
            throw new CategoryException("分类参数级别错误");
        }
        this.categoryList=categoryMapper.selectList(Wrappers.lambdaQuery(Category.class)
                .select(Category::getCat_id,Category::getCat_name,Category::getCat_pid,Category::getCat_level,Category::getCat_deleted));

        List<Category> levelOne;
        levelOne=getCategoryList(Level.LEVEL_ONE.level);
        choseLevel(level, levelOne);
        ResponseResult<List> res=new ResponseResult<>();
        res.setSuccess("获取成功",levelOne);
        return res;
    }

    private void choseLevel(Level level, List<Category> levelOne) throws CategoryException {
        List<Category> levelTwo;
        List<Category> levelThree;
        switch (level){
            case LEVEL_ONE:
                break;
            case LEVEL_TWO:
                levelTwo=getCategoryList(Level.LEVEL_TWO.level);
                mnt(levelOne,levelTwo);
                break;
            case LEVEL_THREE:
                levelTwo=getCategoryList(Level.LEVEL_TWO.level);
                levelThree=getCategoryList(Level.LEVEL_THREE.level);
                mnt(levelOne,levelTwo);
                mnt(levelTwo,levelThree);
                break;
            default:
                throw new CategoryException("分类参数级别错误");
        }
    }

    private void mnt(List<Category> parents, List<Category> children) {
        for (Category parent : parents) {
            List<Category> list=new ArrayList<>();
            for (Category child : children) {
                if (child.getCat_pid().equals(parent.getCat_id())){
                    list.add(child);
                }
            }
            parent.setChildren(list);
        }
    }

    private List<Category> getCategoryList(Integer level){
        List<Category> list=new ArrayList<>();
        for (Category category : this.categoryList) {
            if (category.getCat_level().equals(level)){
                list.add(category);
            }
        }
        return list;
    }

    @Override
    public ResponseResult getPageCategories(Integer type, Integer pagenum, Integer pagesize) throws CategoryException {
        if (type==null){
            type=3;
        }
        Level level= Level.getLevel(type-1);
        if (level==null){
            throw new CategoryException("分类参数级别错误");
        }

        Page<Category> page=new Page<>(pagenum,pagesize);
        List<Category> levelOne;
        page=categoryMapper.selectPage(page,Wrappers.lambdaQuery(Category.class).eq(Category::getCat_level, Level.LEVEL_ONE.level)
                .select(Category::getCat_id,Category::getCat_name,Category::getCat_pid,Category::getCat_level,Category::getCat_deleted));
        levelOne=page.getRecords();
        this.categoryList=categoryMapper.selectList(Wrappers.lambdaQuery(Category.class).select(Category::getCat_id,Category::getCat_name,Category::getCat_pid,Category::getCat_level,Category::getCat_deleted)
                .eq(Category::getCat_level, Level.LEVEL_TWO.level)
                .or().eq(Category::getCat_level, Level.LEVEL_THREE.level));
        choseLevel(level, levelOne);
        ResponseResult<CategoryPageData> res=new ResponseResult<>();
        CategoryPageData<List> pageData=new CategoryPageData<>();
        pageData.setPagenum(page.getCurrent());
        pageData.setPagesize(page.getSize());
        pageData.setTotal(page.getTotal());
        pageData.setResult(levelOne);
        res.setSuccess("获取成功",pageData);
        return res;
    }

    @Override
    public ResponseResult addCategory(Category cate) throws CategoryException {
        ResponseResult<Category> res=new ResponseResult();


        Level level= Level.getLevel(cate.getCat_level());
        if (level==null){
            throw new CategoryException("分类参数级别错误");
        }

        if (cate.getCat_pid()!=0){
            Category category1 = categoryMapper.selectById(cate.getCat_pid());
            if (category1==null){
                throw new CategoryException("父类数据不存在");
            }
            else {
                if (category1.getCat_level()==2){
                    throw new CategoryException("三级分类无法添加新的分类");
                }
                if (category1.getCat_level()>=cate.getCat_level()){
                    throw new CategoryException("父类ID的级别与添加参数的级别错误");
                }
            }
        }


        int insert = categoryMapper.insert(cate);
        if (insert!=0){
            res.setCreated(cate);
        }else{
            res.setUnprocesableEntity("创建分类失败");
        }
        return res;
    }

    @Override
    public ResponseResult selectById(Integer id) {
        ResponseResult<Category> res=new ResponseResult<>();
        Category category = categoryMapper.selectOne(Wrappers.lambdaQuery(Category.class)
                .select(Category::getCat_id,Category::getCat_name,Category::getCat_pid,Category::getCat_level)
                .eq(Category::getCat_id,id));
        if (category!=null){
            res.setSuccess("查询成功",category);
        }else{
            res.setNotFound("查询失败,数据不存在");
        }
        return res;
    }

    @Override
    public ResponseResult updateCategory(Integer id, Category category) {
        ResponseResult<Category> res=new ResponseResult<>();
        if (category.getCat_name()==null||"".equals(category.getCat_name())){
            res.setUnprocesableEntity("所需参数为空");
            return res;
        }
        Category cate = categoryMapper.selectOne(Wrappers.lambdaQuery(Category.class)
                .select(Category::getCat_id,Category::getCat_name,Category::getCat_pid,Category::getCat_level)
                .eq(Category::getCat_id,id));
        if (cate==null){
            res.setNotFound("数据不存在");
            return res;
        }
        cate.setCatName(category.getCat_name());
        int update = categoryMapper.updateById(cate);
        if (update!=0){
            res.setSuccess("更新成功",cate);
        }else{
            res.setNotFound("更新失败");
        }
        return res;
    }

    @Override
    public ResponseResult deleteCategory(Integer id) {
        ResponseResult res=new ResponseResult();
        int delete = categoryMapper.deleteById(id);
        if (delete!=0){
            res.setSuccess("删除成功");
        }else{
            res.setNotFound("删除失败,数据不存在");
        }
        return res;
    }
    
}
